package com.example.seh.person.sell.second;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.example.seh.R;
import com.example.seh.chat.ChatActivity;
import com.example.seh.home.secondhand.GoodsDTO;
import com.example.seh.person.publish.errand.MyErrandAdapter;

import java.util.ArrayList;
import java.util.List;

public class SellAdapter extends BaseAdapter {
    private Context context;
    private int item;
    private List<OrderDTO> lists = new ArrayList<>();
    private String[] pics;

    public SellAdapter(Context context, int item, List<OrderDTO> lists) {
        this.context = context;
        this.item = item;
        this.lists = lists;
    }

    @Override
    public int getCount() {
        if (lists != null) {
            return lists.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (lists != null) {
            return lists.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null){
            convertView = LayoutInflater.from(context).inflate(item, null);
            viewHolder = new ViewHolder();
            viewHolder.imageView = convertView.findViewById(R.id.sell_pic);
            viewHolder.title = convertView.findViewById(R.id.sell_title);
            viewHolder.price = convertView.findViewById(R.id.sell_price);
            viewHolder.callBuyers = convertView.findViewById(R.id.callBuyers);
            viewHolder.buyersAddress = convertView.findViewById(R.id.buyers_address);
            viewHolder.sellState = convertView.findViewById(R.id.sell_state);

            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.title.setText(lists.get(position).getGoodsDTO().getGoodsName());
        viewHolder.price.setText(lists.get(position).getGoodsDTO().getGoodsPrice()+"");
        viewHolder.buyersAddress.setText(lists.get(position).getReceiveAddress());

        if (lists.get(position).getOrderStatus() == 1){
            viewHolder.sellState.setText("交易中");
        }else if (lists.get(position).getOrderStatus() == 2){
            viewHolder.sellState.setText("交易完成");
        }

        if (lists.get(position).getGoodsDTO().getGoodsPic() != null){
            pics = lists.get(position).getGoodsDTO().getGoodsPic().split(";");
            if (!pics[0].isEmpty()) {

                Glide.with(context)
                        .load(pics[0])
                        .diskCacheStrategy(DiskCacheStrategy.NONE)
                        .dontAnimate()
                        .listener(new RequestListener<Drawable>() {
                            @Override
                            public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                                return false;
                            }

                            @Override
                            public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                                if (target instanceof ImageView) {
                                    ((ImageView) target).setTag(pics[0]);  // 绑定加载请求与视图
                                }
                                return false;
                            }
                        })
                        .into(viewHolder.imageView);
            } else {
                Glide.with(context)
                        .clear(viewHolder.imageView);  // 清除之前的加载请求，避免复用时显示错误的图片
                viewHolder.imageView.setImageResource(R.drawable.wtp);  // 或者设置默认占位图像
            }
        }else {
            viewHolder.imageView.setImageResource(R.drawable.wtp);
        }

        viewHolder.callBuyers.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int buyerId = lists.get(position).getUserId();
                Log.e("bought",lists.get(position).getUserDTO().toString());
                Intent intent = new Intent(context, ChatActivity.class);
                intent.putExtra("user", lists.get(position).getUserDTO());
                context.startActivity(intent);
            }
        });

        return convertView;
    }

    class ViewHolder{
        ImageView imageView;
        TextView title,price,sellState,buyersAddress,callBuyers;
    }

}
